package rxjava2.groccery;



import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * Created by SEELE on 2018/7/31.
 */
public class MD5 {

//    static final int BUFFER_SIZE = Integer.getInteger("md5-size");

    static {
        System.setProperty("md5-size", "120");
        System.out.println("MD----Static");
    }

    public static void main(String[] args) {
        String md5 = getMD5(new File("F:\\DiskE\\AndroidStudioProject\\Client" +
                "\\app\\build\\outputs\\apk\\release\\jiquV2.2.apk"));

        System.out.println(md5);
    }

    private static final String TAG = "MD5";

    //md5用
    private static final char HEX_DIGITS[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
            'A', 'B', 'C', 'D', 'E', 'F'};

    private MD5() {
        //no instance
    }


    public static String toHexString(byte[] b) {
        StringBuilder sb = new StringBuilder(b.length * 2);
        for (int i = 0; i < b.length; i++) {
            sb.append(HEX_DIGITS[(b[i] & 0xf0) >>> 4]);
            sb.append(HEX_DIGITS[b[i] & 0x0f]);
        }
        return sb.toString();
    }

    public static String getMD5(File fileName) {
        InputStream fis;
        byte[] buffer = new byte[1024];
        int numRead;
        MessageDigest md5;
        try {
            fis = new FileInputStream(fileName);
            md5 = MessageDigest.getInstance("MD5");
            while ((numRead = fis.read(buffer)) > 0) {
                md5.update(buffer, 0, numRead);
            }
            fis.close();
            return toHexString(md5.digest());
        } catch (Exception e) {
            return null;
        }
    }

    public static String getMD5(String source) {
        MessageDigest md5;
        try {
            md5 = MessageDigest.getInstance("MD5");
            md5.update(source.getBytes());
            return toHexString(md5.digest());
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "abcedfghijklmnopqrstuvwxyz123456";
    }
}
